from fastapi import Request
from sqlalchemy.orm import Session

from core.dao.cms_gaode_trace_dao import CmsGaodeTraceDao
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsGaodeTraceVO
from core.service.gaode.gaode_trace_service import GaodeTraceService


class CmsGaodeTraceService:
    """
    高德终端service
    """

    @classmethod
    def get_cms_gaode_trace_by_id(cls, result_db: Session, id: int):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param id: id
        :return: 参数配置列表信息对象
        """
        cms_gaode_trace = CmsGaodeTraceDao.get_cms_gaode_trace_by_id(result_db, id)

        return cms_gaode_trace

    @classmethod
    def get_cms_gaode_trace_by_trace_id(cls, result_db: Session, trace_id: str):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param trace_id: 高德终端id
        :return: 参数配置列表信息对象
        """
        cms_gaode_trace_list = CmsGaodeTraceDao.get_cms_gaode_trace_by_trace_id(result_db, trace_id)
        return cms_gaode_trace_list

    @classmethod
    def get_cms_gaode_trace_like_name(cls, result_db: Session, trace_name: str):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param trace_name: 高德终端id
        :return: 参数配置列表信息对象
        """
        cms_gaode_trace_list = CmsGaodeTraceDao.get_cms_gaode_trace_like_name(result_db, trace_name)

        return cms_gaode_trace_list

    @classmethod
    async def add_cms_gaode_trace(cls, request: Request, result_db: Session, cms_gaode_trace: CmsGaodeTraceVO):
        """
        新增参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_gaode_trace: 新增参数配置对象
        :return: 新增参数配置校验结果
        """
        result = GaodeTraceService.add_track(cms_gaode_trace.service_id, cms_gaode_trace.terminal_id,
                                             cms_gaode_trace.trace_name)
        if result['errcode'] == 10000:
            try:
                cms_gaode_trace.trace_id = result['data']['trid']
                CmsGaodeTraceDao.add_cms_gaode_trace_dao(result_db, cms_gaode_trace)
                result_db.commit()
                result = dict(is_success=True, message='新增成功')
            except Exception as e:
                result_db.rollback()
                result = dict(is_success=False, message=str(e))
        else:
            result = dict(is_success=False, message=str(result['errmsg']))
        return CrudResponse(**result)

    @classmethod
    async def search_cms_gaode_trace(cls, request: Request, result_db: Session, cms_gaode_trace: CmsGaodeTraceVO):
        """
        查询轨迹信息，修改轨迹里程、时间
        :param request: Request对象
        :param result_db: orm对象
        :param cms_gaode_trace: 新增参数配置对象
        :return: 新增参数配置校验结果
        """
        result = GaodeTraceService.search_track(cms_gaode_trace.service_id, cms_gaode_trace.terminal_id,
                                             cms_gaode_trace.trace_id)
        print(result)
        if result['errcode'] == 10000:
            try:
                # 根据轨迹编码查询轨迹方式只会返回一条轨迹，故使用该方法
                cms_gaode_trace.distance = result['data']['tracks'][0]['distance']
                cms_gaode_trace.time = result['data']['tracks'][0]['distance']
                cms_gaode_trace.count = result['data']['tracks'][0]['counts']
                CmsGaodeTraceDao.update_cms_gaode_trace_dao(result_db, cms_gaode_trace)
                result_db.commit()
                result = dict(is_success=True, message='新增成功')
            except Exception as e:
                result_db.rollback()
                result = dict(is_success=False, message=str(e))
        else:
            result = dict(is_success=False, message=str(result['errmsg']))
        return CrudResponse(**result)


    @classmethod
    def delete_cms_gaode_trace(cls, result_db: Session,  id: int, service_id: str, terminal_id: str, trace_id: str):
        """
        删除菜单信息service
        :param result_db: orm对象
        :param id: id
        :return: 删除终端
        """
        if id > 0:
            result = GaodeTraceService.delete_track(service_id, terminal_id, trace_id)
            if result['errcode'] == 10000:
                try:
                    CmsGaodeTraceDao.delete_cms_gaode_trace_dao(result_db, id)
                    result_db.commit()
                    result = dict(is_success=True, message='删除成功')
                except Exception as e:
                    result_db.rollback()
                    result = dict(is_success=False, message=str(e))
            else:
                result = dict(is_success=False, message=str(result['errmsg']))
        else:
            result = dict(is_success=False, message='传入高德终端id为空')
        return CrudResponse(**result)

    @classmethod
    def get_cms_gaode_trace_by_execute_id(cls, result_db: Session, trace_id: str):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param trace_id: 高德终端id
        :return: 参数配置列表信息对象
        """
        cms_gaode_trace = CmsGaodeTraceDao.get_cms_gaode_trace_by_execute_id(result_db, trace_id)
        return cms_gaode_trace

